package com.douqu.game.activity.server;


import com.douqu.game.core.e.E_ProfileVersion;
import com.douqu.game.core.factory.ConstantFactory;
import com.douqu.game.core.util.LogUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.core.task.TaskExecutor;

/**
 * Created by bean
 * 2017-03-23 16:58.
 */
public class SpringContext
{
    /**
     * spring管理器
     */
    private static ApplicationContext applicationContext;

    public static void init(ApplicationContext ac)
    {
        applicationContext = ac;
    }

    public static <T> T getBean(Class cls)
    {
        if(applicationContext == null)
            return null;

        return (T) applicationContext.getBean(cls);
    }

    public static <T> T getBean(String name)
    {
        return (T) applicationContext.getBean(name);
    }

    public static TaskExecutor getTaskExecutor()
    {
        return SpringContext.getBean(ConstantFactory.TASK_EXECUTOR_NAME);
    }

    public static void stop()
    {

        SpringApplication.exit(applicationContext);
    }

    public static String getProfile()
    {
        Environment env = getBean(Environment.class);
        return env.getProperty("profile");
    }

//    public static String getBattleServerHost()
//    {
//        CommonConfig commonConfig = SpringContext.getBean(CommonConfig.class);
//        return commonConfig.getBattleServerHost();
//    }
//
//    public static String getAuthServerHost()
//    {
//        CommonConfig commonConfig = SpringContext.getBean(CommonConfig.class);
//        return commonConfig.getAuthServerHost();
//    }
//    public static String getRMIServerHost()
//    {
//        CommonConfig commonConfig = SpringContext.getBean(CommonConfig.class);
//        return commonConfig.getRmiServerHost();
//    }
//    public static int getRMIServerPort()
//    {
//        CommonConfig commonConfig = SpringContext.getBean(CommonConfig.class);
//        return commonConfig.getRmiServerPort();
//    }
//    public static int getTCPServerPort()
//    {
//        CommonConfig commonConfig = SpringContext.getBean(CommonConfig.class);
//        return commonConfig.getTcpServerPort();
//    }

    public static String getHTTPServerPort()
    {
        Environment env = getBean(Environment.class);
        return env.getProperty("server.port");
    }

    public static void showConfig()
    {
        Environment env = getBean(Environment.class);
        LogUtils.info("VERSION ------------>" + E_ProfileVersion.forKey(env.getProperty("profile")));
        LogUtils.info("DBUrl--------------->" + env.getProperty("spring.datasource.url"));
    }
}
